home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / SpriteWorld 2.2 / SpriteWorld files / Utils / SWGameUtils.c < prev    next >
Encoding:
Text File  |  1999-01-25  |  10.3 KB  |  349 lines  |  [TEXT/CWIE]

  1. ///--------------------------------------------------------------------------------------
  2. //    SWGameUtils.c
  3. //
  4. //    Portions are copyright: © 1991-94 Tony Myles, All rights reserved worldwide.
  5. //
  6. //    Description:    some utility functions for games
  7. ///--------------------------------------------------------------------------------------
  8.  
  9.  
  10. #include <QDOffscreen.h>
  11. #include <Palettes.h>
  12. #include <SWCommonHeaders.h>
  13. #include <SWGameUtils.h>
  14.  
  15.  
  16. ///--------------------------------------------------------------------------------------
  17. //    Randomize - initialize random number seed
  18. ///--------------------------------------------------------------------------------------
  19.  
  20. void Randomize( void )
  21. {
  22.     GetDateTime( (unsigned long *)(&qd.randSeed) );
  23. }
  24.  
  25.  
  26. ///--------------------------------------------------------------------------------------
  27. //    GetRandom - generate a random number between min and max inclusive
  28. ///--------------------------------------------------------------------------------------
  29.  
  30. short GetRandom(short min, short max)
  31. {
  32.     unsigned short        random;
  33.     long                range, temp;
  34.  
  35.     random = Random();
  36.     range = (max - min) + 1;
  37.     temp = (random * range) / 65536;
  38.     random = temp + min;
  39.  
  40.     return random;
  41. }
  42.  
  43.  
  44. ///--------------------------------------------------------------------------------------
  45. //    CenterRect - centers rectA in rectB, returning result in rectA
  46. ///--------------------------------------------------------------------------------------
  47.  
  48. void CenterRect(Rect* rectA, Rect* rectB)
  49. {
  50.     short width = (rectA->right - rectA->left);
  51.     short height = (rectA->bottom - rectA->top);
  52.  
  53.     rectA->left = rectB->left + (((rectB->right - rectB->left) / 2) - (width / 2));
  54.     rectA->top = rectB->top + (((rectB->bottom - rectB->top) / 2) - (height / 2));
  55.     rectA->right = rectA->left + width;
  56.     rectA->bottom = rectA->top + height;
  57. }
  58.  
  59.  
  60. ///--------------------------------------------------------------------------------------
  61. //    AlignRect - aligns the rectangle so its left side is positioned on an even long word
  62. //    boundary. This would be every 4th pixel in 8-bit, and every 2nd in 16-bit. However,
  63. //    we just do every 4th pixel regardless of depth, since it's easier that way. :-)
  64. //    Besides, it means our rect will be aligned to double boundaries when in 16-bit.
  65. ///--------------------------------------------------------------------------------------
  66.  
  67. void    AlignRect(Rect* rectP)
  68. {
  69.     short    rectWidth, overflow;
  70.     
  71.     rectWidth = rectP->right - rectP->left;
  72.     overflow = rectP->left & 3;            // Save right two bits
  73.     
  74.     rectP->left = rectP->left>>2<<2;    // Eliminate right two bits (numbers 1-3)
  75.     
  76.     if (overflow > 2)                    // Round up if closer to right than to left
  77.         rectP->left += 4;                // (just a nice touch)
  78.     
  79.     rectP->right = rectP->left + rectWidth;
  80. }
  81.  
  82.  
  83.  
  84. short        gOldEventMask;    // Used by AllowKeyUpEvents and RestoreEventMask
  85. Boolean        eventMaskIsGood = false;
  86.  
  87. ///--------------------------------------------------------------------------------------
  88. //    AllowKeyUpEvents - allows keyUpEvents. Make sure to call RestoreEventMask
  89. //    before your program quits if you call AllowKeyUpEvents.
  90. ///--------------------------------------------------------------------------------------
  91.  
  92. void AllowKeyUpEvents( void )
  93. {
  94.     gOldEventMask = LMGetSysEvtMask();
  95.     SetEventMask(everyEvent);
  96.     
  97.         // Let RestoreEventMask know that the old mask has been saved
  98.     eventMaskIsGood = true;
  99. }
  100.  
  101.  
  102. ///--------------------------------------------------------------------------------------
  103. //    RestoreEventMask - call this before your program quits if you previously
  104. //    called AllowKeyUpEvents. This will beep if you didn't call AllowKeyUpEvents first.
  105. ///--------------------------------------------------------------------------------------
  106.  
  107. void RestoreEventMask( void )
  108. {
  109.     if (eventMaskIsGood)
  110.         SetEventMask(gOldEventMask);
  111.     else
  112.         SysBeep(1);
  113. }
  114.  
  115.  
  116.  
  117. ///--------------------------------------------------------------------------------------
  118. //    Globals for HideMenuBar and ShowMenuBar
  119. ///--------------------------------------------------------------------------------------
  120.         
  121. RgnHandle    gOldVisRgn = NULL;    // visRgn of window before hiding menu bar
  122. RgnHandle    gUpdateRgn = NULL;    // region returned to user
  123. short        gOldMBarHeight;
  124.  
  125.  
  126. ///--------------------------------------------------------------------------------------
  127. //    SWHideMenuBar - expands the vis region of grafPort to cover the entire window, which
  128. // will allow you to draw in the top of that window to erase the menu bar. This is a
  129. // simple routine designed for programs with only one window that covers the menu bar.
  130. // If you need to expand the region of more than one window, you need a different routine.
  131. // Be sure to make the window visible before calling this. HideMenuBar returns the
  132. // region of the menu bar and corners of the screen, in case you want to erase or
  133. // draw in that area. Remember to dispose this region when done with it!
  134. ///--------------------------------------------------------------------------------------
  135.  
  136. RgnHandle    SWHideMenuBar(GrafPtr grafPort)
  137. {
  138.     RgnHandle newVisRgn;
  139.     GrafPtr savePort;
  140.     
  141.     if (gOldVisRgn != NULL)
  142.         return NULL;
  143.  
  144.     GetPort(&savePort);
  145.     SetPort(grafPort);
  146.     
  147.     gOldMBarHeight = LMGetMBarHeight();
  148.     LMSetMBarHeight( 0 );        // Keeps things like SuperClock from coming on.
  149.  
  150.         // save off vis region
  151.     gOldVisRgn = NewRgn();
  152.     CopyRgn(grafPort->visRgn, gOldVisRgn);
  153.  
  154.         // expand the vis region of the port rect to be completely rectangular
  155.     newVisRgn = NewRgn();
  156.     RectRgn(newVisRgn, &grafPort->portRect);
  157.     CopyRgn(newVisRgn, grafPort->visRgn);
  158.     DisposeRgn(newVisRgn);
  159.     
  160.         // dispose gUpdateRgn from previous call to HideMenuBar in case user forgot to
  161.     if (gUpdateRgn != NULL)
  162.     {
  163.         DisposeRgn(gUpdateRgn);
  164.         gUpdateRgn = NULL;
  165.     }
  166.     
  167.         // set gUpdateRgn to region of rounded corners and menu bar
  168.     gUpdateRgn = NewRgn();
  169.     CopyRgn(gOldVisRgn, gUpdateRgn);
  170.     DiffRgn(grafPort->visRgn, gUpdateRgn, gUpdateRgn);
  171.  
  172.     SetPort(savePort);
  173.     return gUpdateRgn;
  174. }
  175.  
  176.  
  177. ///--------------------------------------------------------------------------------------
  178. //    KeepMenuBarHidden - call this during your main animation loop to make sure the
  179. //    window's visRgn stays the same as the window's portRect. (Utilities like the Control
  180. //    strip can mess it up.) Only necessary if you use CopyBits as your screenDrawProc,
  181. //    and you don't hide the Control Strip before starting your animation.
  182. ///--------------------------------------------------------------------------------------
  183.  
  184. void KeepMenuBarHidden(GrafPtr grafPort)
  185. {
  186.     Rect    regionRect;
  187.     
  188.     regionRect = (**grafPort->visRgn).rgnBBox;
  189.     
  190.         // Fix the region only if it needs fixing
  191.     if (regionRect.top != grafPort->portRect.top || 
  192.         regionRect.bottom != grafPort->portRect.bottom ||
  193.         regionRect.left != grafPort->portRect.left ||
  194.         regionRect.right != grafPort->portRect.right )
  195.     {
  196.         RectRgn(grafPort->visRgn, &grafPort->portRect);
  197.     }
  198. }
  199.  
  200.  
  201. ///--------------------------------------------------------------------------------------
  202. //    SWShowMenuBar - restores the grafPort to the way it was before the call to HideMenuBar.
  203. //    Make sure to call this after every call to HideMenuBar to dispose of gOldVisRgn.
  204. ///--------------------------------------------------------------------------------------
  205.  
  206. void SWShowMenuBar(GrafPtr grafPort)
  207. {
  208.     GrafPtr savePort;
  209.     RgnHandle junkRgn;
  210.     
  211.     if (gOldVisRgn == NULL)
  212.         return;
  213.  
  214.     GetPort(&savePort);
  215.     SetPort(grafPort);
  216.     
  217.     LMSetMBarHeight( gOldMBarHeight );
  218.  
  219.         // fill the rounded corners of the screen with black again
  220.     junkRgn = NewRgn();
  221.     CopyRgn(gOldVisRgn, junkRgn);
  222.     DiffRgn(grafPort->visRgn, junkRgn, junkRgn);
  223.     ForeColor(blackColor);
  224.  
  225.     FillRgn(junkRgn, &qd.black);
  226.     
  227.     DisposeRgn(junkRgn);
  228.  
  229.         // restore the old vis region
  230.     CopyRgn(gOldVisRgn, grafPort->visRgn);
  231.     DisposeRgn(gOldVisRgn);
  232.     gOldVisRgn = NULL;
  233.  
  234.         // Dispose this region which was created by SWHideMenuBar
  235.     if (gUpdateRgn != NULL)
  236.     {
  237.         DisposeRgn(gUpdateRgn);
  238.         gUpdateRgn = NULL;
  239.     }
  240.  
  241.     DrawMenuBar();
  242.     
  243.     SetPort(savePort);
  244. }
  245.  
  246.  
  247. ///--------------------------------------------------------------------------------------
  248. //    GetDepthFromGlobalRect
  249. ///--------------------------------------------------------------------------------------
  250.  
  251. short GetDepthFromGlobalRect( Rect* globalRect )
  252. {
  253.     SysEnvRec        theSERec;
  254.     GDHandle        rectGDH;
  255.     short            depth;
  256.     
  257.     
  258.     SysEnvirons( 2, &theSERec );
  259.     if ( !theSERec.hasColorQD )                // no colorQD?
  260.     {
  261.         depth = 1;
  262.     }
  263.     else
  264.     {    
  265.         rectGDH = GetMaxDevice( globalRect );
  266.         depth = GetGDeviceDepth( rectGDH );        
  267.     }
  268.     return depth;
  269. }
  270.  
  271.  
  272. ///--------------------------------------------------------------------------------------
  273. //    GetDepthFromWindow
  274. ///--------------------------------------------------------------------------------------
  275.  
  276. short GetDepthFromWindow( WindowPtr theWindow )
  277. {
  278.     SysEnvRec        theSERec;
  279.     GWorldPtr        oldGWorld, windowGWld;
  280.     GDHandle        oldGDH, windowGDH;
  281.     short            depth;
  282.     
  283.     
  284.     SysEnvirons( 2, &theSERec );
  285.     if ( !theSERec.hasColorQD )                // no colorQD?
  286.         depth = 1;
  287.     else
  288.     {    
  289.         GetGWorld( &oldGWorld, &oldGDH );
  290.         SetPort( theWindow );
  291.         GetGWorld( &windowGWld, &windowGDH );
  292.         
  293.         depth = GetGDeviceDepth( windowGDH );
  294.         
  295.         SetGWorld( oldGWorld, oldGDH );
  296.     }
  297.     return depth;
  298. }
  299.  
  300.  
  301. ///--------------------------------------------------------------------------------------
  302. //    GetGDeviceDepth - to get the depth of the main monitor,
  303. //    simply do this: GetGDeviceDepth( GetMainDevice() );
  304. ///--------------------------------------------------------------------------------------
  305.  
  306. short GetGDeviceDepth( GDHandle theGDH )
  307. {
  308.     SysEnvRec        theSERec;
  309.     PixMapHandle    thePixMap;
  310.     
  311.     
  312.     SysEnvirons( 2, &theSERec );
  313.     if ( !theSERec.hasColorQD )                // no colorQD?
  314.         return 1;
  315.     else
  316.     {
  317.         thePixMap = (**theGDH).gdPMap;
  318.         return (**thePixMap).pixelSize;     // Depth in bits (1,2,4...)
  319.     }
  320. }
  321.  
  322.  
  323. ///--------------------------------------------------------------------------------------
  324. //    RestoreSystemPalette - if your game uses a custom palette, call this when switching
  325. //    to the Finder or some other app if you want it to have its original palette back.
  326. ///--------------------------------------------------------------------------------------
  327.  
  328. void    RestoreSystemPalette( void )
  329. {
  330.     RestoreDeviceClut(nil);
  331.     PaintBehind(FrontWindow(), GetGrayRgn());
  332.     PaintOne(nil, GetGrayRgn());
  333.     DrawMenuBar();
  334. }
  335.  
  336.  
  337. ///--------------------------------------------------------------------------------------
  338. //    HasSystem7
  339. ///--------------------------------------------------------------------------------------
  340.  
  341. Boolean HasSystem7( void )
  342. {
  343.     long    gestaltResponse;
  344.     OSErr    err;
  345.  
  346.     err = Gestalt( gestaltSystemVersion, &gestaltResponse );
  347.     return ( err == noErr && (gestaltResponse >= 0x0700));
  348. }
  349.